﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.SettlementInfoModel;
using System.Linq;
using FwProject.Core.Distribution;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using System.Net.Http;
using System.Threading.Tasks;
using Abp.RemoteEventBus;
using FwProject.Comm.Sms;
using FwProject.Comm;

namespace FwProject.BackGround.DistributionEnter
{
    public class PublicSendWebProbeWorkBackGround : PeriodicBackgroundWorkerBase, ISingletonDependency
    {

        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public ILogger logger { get; set; }
        public PublicSendWebProbeWorkBackGround(AbpTimer timer, IRepository<DistributionInfo, Guid> repositoryDistributionInfo):base(timer) {
            remoteEventBus = NullRemoteEventBus.Instance;
#if DEBUG
            Timer.Period = 1000 * 60*20;
#else
           Timer.Period = 1000 * 60 *20;
#endif
            logger = NullLogger.Instance;
        }
        [UnitOfWork]
        protected override void DoWork()
        {
            using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
            {
                using (HttpClient client = new HttpClient())
                {
                    try
                    {
                        using (HttpResponseMessage responseMessage = Task.Run(() => client.GetAsync("https://service.fanwanyigou.com/api/WebProbe/callBackMessage")).Result)
                        {
                            Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
                            string msg = Encoding.UTF8.GetString(resultBytes);
                            if (Convert.ToBoolean(msg))
                            {
                                logger.Info($"点碗系统服务检查：{msg}");
                            }

                            else {
                                send();
                            }
                               

                        }
                        using (HttpResponseMessage responseMessage = Task.Run(() => client.GetAsync("https://shop.fanwanyigou.com/api/WebProbe/callBackMessage")).Result)
                        {
                            Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
                            string msg = Encoding.UTF8.GetString(resultBytes);
                            logger.Info($"点碗商城服务检查：{msg}");
                        }
                    }
                    catch (Exception exe)
                    {
                        send();
                        logger.Error($"服务已关闭",exe);
                    }
                  

                    ;
                }

             CurrentUnitOfWork.SaveChanges();
            }
        }
        private void send() {

            SmsObject sms = new SmsObject();
            sms.code = $"120";
            sms.Mobile = "13206048151";
            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TypeSmsObject)
            {
                Data = { [RemoteTypeHelp.SmsObject] = sms }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TopicSms, catcheventDate);
        }
    }
}
